﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.Practices.Unity;
using Vit.Web.Services;
using Vit.Web.Models;

namespace Vit.Web.Mvc
{
    public class SiteHttpModule : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            VitApplication application = sender as VitApplication;
            if(application == null)
                return;
            IUnityContainer unity = application.UnityContainer;
            ISiteService siteSvr = unity.Resolve<ISiteService>();
            WebSite site = null;
            if (siteSvr.GetMappingSite(application.Context.Request.Url.OriginalString, out site))
            {
                application.CurrentSite = site;
            }
        }

        #endregion
    }
}
